﻿using Elev.Adapter;
using Elev.API.DouBan;
using Elev.DataFactory;
using Elev.Farmworker.Utility;
using Elev.MoviesCenter.Entity;
using Elev.MoviesCenter.Utility;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Elev.MoviesCenter.Controls
{
    /// <summary>
    /// UC_ImportMovie.xaml 的交互逻辑
    /// </summary>
    public partial class UC_ImportMovie : UserControl
    {
        private List<VideoInfoEntity> videoList = null;

        public UC_ImportMovie()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            List<NodeItemsEntity> Nodes = new List<NodeItemsEntity>();
            string[] dirs = Directory.GetLogicalDrives();
            foreach (string dirName in dirs)
            {
                NodeItemsEntity node = new NodeItemsEntity()
                {
                    Name = dirName,
                    FullPath = dirName,
                    NodeItem = GetNode(dirName)
                };
                Nodes.Add(node);
            }
            treeDic.ItemsSource = Nodes;
        }

        public List<NodeItemsEntity> GetNode(string dirPath)
        {
            List<NodeItemsEntity> nodes = new List<NodeItemsEntity>();
            DirectoryInfo di = new DirectoryInfo(dirPath);
            DirectoryInfo[] diChild = di.GetDirectories();
            {
                for (int i = 0; i < diChild.Length; i++)
                {
                    if (!(diChild[i].Attributes.HasFlag(FileAttributes.ReadOnly) ||
                        diChild[i].Attributes.HasFlag(FileAttributes.Hidden)))
                    {
                        DirectorySecurity s = new DirectorySecurity(diChild[i].FullName, AccessControlSections.Access);
                        if (!s.AreAccessRulesProtected)
                        {
                            NodeItemsEntity node = new NodeItemsEntity();
                            node.Name = diChild[i].Name;
                            node.FullPath = diChild[i].FullName;
                            node.NodeItem = GetNode(diChild[i].FullName);
                            nodes.Add(node);
                        }
                    }
                }
            }
            return nodes;
        }

        private void treeDic_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            NodeItemsEntity item = treeDic.SelectedValue as NodeItemsEntity;
            videoList = Common.LoadVideoInfo(item.FullPath);
            dgVideoList.ItemsSource = videoList;
        }

        private void dgVideoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgVideoList.SelectedIndex > 0)
            {
                var value = (dgVideoList.CurrentColumn.GetCellContent(dgVideoList.CurrentItem) as CheckBox);
                if (value != null) { value.IsChecked = !value.IsChecked; }
                VideoInfoEntity select = dgVideoList.SelectedItem as VideoInfoEntity;
                tbFileName.Text = select.FileName;
            }
        }

        private void btnFileAnalysis_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbVideoName.Text)) { MessageBox.Show("视频名称为空"); return; }
            string result = DouBanAdapter.SelectVideoKist(tbVideoName.Text);
            MovieSearch movieSearch = JsonHelper.DeserializeJsonToObject<MovieSearch>(result);
            combSearchList.ItemsSource = movieSearch.subjects;
        }

        private void btnSaveMovieInfo_Click(object sender, RoutedEventArgs e)
        {
            TB_FilesInfo fileInfo = dgVideoList.SelectedItem as TB_FilesInfo;
            string selectID = combSearchList.SelectedValue.ToString();
            string result = DouBanAdapter.GetVideoInfo(selectID);
            Movies selectMovies = JsonHelper.DeserializeJsonToObject<Movies>(result);
            TB_Videos video = Common.VideoConvert(selectMovies);
            LocalCenter.DBEntities.TB_Videos.Add(video);

            TB_FilesInfo file = new TB_FilesInfo();
            file.Guid = Guid.NewGuid();
            //file.DirectoryGuid = Common.GetDirectoryGuid();
            file.VideoGuid = video.Guid;
            file.FileName = tbVideoName.Text;
            file.EpisodeName = tbMovieName.Text;
            LocalCenter.DBEntities.TB_FilesInfo.Add(file);
            if (LocalCenter.DBEntities.SaveChanges() > 0)
            {
                MessageBox.Show("保存成功！");
            }
            else
            {
                MessageBox.Show("保存失败！");
            }
        }

        private void combSearchList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectID = combSearchList.SelectedValue.ToString();
            string result = DouBanAdapter.GetVideoInfo(selectID);
            Movies selectMovies = JsonHelper.DeserializeJsonToObject<Movies>(result);
            txtVideoName.Text = selectMovies.title;
            txtVideoOriginalTitle.Text = selectMovies.original_title;
            txtVideoType.Text = selectMovies.subtype;
            txtVideoImages.Source = new BitmapImage(new Uri(selectMovies.images.large, UriKind.RelativeOrAbsolute));
            if (selectMovies.languages != null)
            {
                foreach (string strLanguage in selectMovies.languages)
                {
                    txtVideoLanguage.Text += strLanguage + "、";
                }
            }
            if (selectMovies.durations != null)
            {
                foreach (string strDurations in selectMovies.durations)
                {
                    txtVideoTime.Text += strDurations + "、";
                }
            }
            if (selectMovies.casts != null)
            {
                foreach (CastsItem casts in selectMovies.casts)
                {
                    txtVideoCasts.Text += casts.name + "、";
                }
            }
        }

        private void btnAnalysisAll_Click(object sender, RoutedEventArgs e)
        {
            var list = dgVideoList.ItemsSource as List<VideoInfoEntity>;
            Task.Run(() =>
            {
                foreach (var videoEntity in list)
                {
                    videoEntity.AnalysisResult = false;
                    if (!string.IsNullOrEmpty(videoEntity.VideoNameName))
                    {
                        string result = DouBanAdapter.SelectVideoKist(videoEntity.VideoNameName);
                        MovieSearch movieSearch = JsonHelper.DeserializeJsonToObject<MovieSearch>(result);
                        if (movieSearch != null && movieSearch.subjects != null && movieSearch.subjects.Length > 0)
                        {
                            string response = DouBanAdapter.GetVideoInfo(movieSearch.subjects[0].id);
                            Movies selectMovies = JsonHelper.DeserializeJsonToObject<Movies>(response);
                            videoEntity.VideoEntity = Common.VideoConvert(selectMovies);
                            videoEntity.AnalysisResult = true;
                        }
                    }
                }
            });
        }

        private void btnSaveAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var one in dgVideoList.Items)
            {
                CheckBox cb = dgVideoList.Columns[0].GetCellContent(one) as CheckBox;
                if (cb != null && cb.IsChecked == true)
                {
                    TB_FilesInfo file = new TB_FilesInfo();
                }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dgVideoList_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {

        }
    }
}
